package com.proyectofinal.ramas.service;

import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import com.proyectofinal.ramas.domain.User;
import java.util.ArrayList;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Service for processing Users
 * 
 */
@Service("userService")
@Transactional
public class UserService {

	protected static Logger logger = Logger.getLogger("service");
	
	@Resource(name="sessionFactory")
	private SessionFactory sessionFactory;
	
	/**
	 * Retrieves all users
	 * 
	 * @return a list of users
	 */
	public List<User> getAll() {
		logger.debug("Retrieving all persons");
		
		// Retrieve session from Hibernate
		Session session = sessionFactory.getCurrentSession();
		
		// Create a Hibernate query (HQL)
		Query query = session.createQuery("FROM  User");
		
		// Retrieve all
		return  query.list();
	}
        /**
	 * Retrieves a single user
	 */
	public User get( String userName ) {
                
                Session session = sessionFactory.getCurrentSession();
                
		List<User> userList = new ArrayList<User>();
		Query query = session.createQuery("FROM User WHERE userName = :userName");
		query.setParameter("userName", userName);
		userList = query.list();
		if (userList.size() > 0)
			return userList.get(0);
		else
			return null;	
	}
          /**
	 * Retrieves a single user
	 */
	public User get( String userName, String password ) {
                
                Session session = sessionFactory.getCurrentSession();
                
		List<User> userList = new ArrayList<User>();
		Query query = session.createQuery("FROM User WHERE userName = :userName and password = :password");
		query.setParameter("userName", userName);
                query.setParameter("password", password);
		userList = query.list();
		if (userList.size() > 0)
			return userList.get(0);
		else
			return null;	
	}
        
	
	/**
	 * Retrieves a single user
	 */
	public User get( Integer userID ) {
		// Retrieve session from Hibernate
		Session session = sessionFactory.getCurrentSession();
		
		// Retrieve existing user first
		User user = (User) session.get(User.class, userID);
		
		return user;
	}
	/**
	 * Adds a new user
	 */
	public void add(User user) {
		logger.debug("Adding new user");
		
		// Retrieve session from Hibernate
		Session session = sessionFactory.getCurrentSession();
		
		// Save
		session.save(user);
	}
	
	/**
	 * Deletes an existing user
	 * @param userID the userID of the existing user
	 */
	public void delete(Integer userID) {
		logger.debug("Deleting existing person");
		
		// Retrieve session from Hibernate
		Session session = sessionFactory.getCurrentSession();
		
		// Retrieve existing user first
		User user = (User) session.get(User.class, userID);
		
		// Delete 
		session.delete(user);
	}
	
	/**
	 * Edits an existing user
	 */
	public void edit(User user) {
		logger.debug("Editing existing person");
		
		// Retrieve session from Hibernate
		Session session = sessionFactory.getCurrentSession();
		
		// Retrieve existing user via userID
		User existingUser = (User) session.get(User.class, user.getUserID());
		
		// Assign updated values to this user
		existingUser.setFirstName(user.getFirstName());
		existingUser.setLastName(user.getLastName());

		// Save updates
		session.save(existingUser);
	}
}
